//
//  JJTabBar.swift
//  JJSina
//
//  Created by mac on 2017/11/30.
//  Copyright © 2017年 xinyuan. All rights reserved.
//

import UIKit
protocol JJTabBarDelegate: NSObjectProtocol {
    // 定义协议方法
    func composeButtonDidSelected()
}

protocol QWDelegate: NSObjectProtocol {
    // 定义协议方法
    func cbtn()
}
class JJTabBar: UITabBar {

    // 声明代理
    weak var JJdelegate: JJTabBarDelegate?
    // 声明代理
    weak var QWDelegate: QWDelegate?
    
    var clouse: (()->())?
    // 声明闭包
    var colouse: (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeButton)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 按钮点击事件
    @objc func buttonClick() {
        print("点击")
        clouse?()
        //
        JJdelegate?.composeButtonDidSelected()
        QWDelegate?.cbtn()
        //调用闭包
        colouse?()
    }
    // 设置按钮位置
    override func layoutSubviews() {
        super.layoutSubviews()
        // 设置子控制器的位置
        let  w = JJ_SCREEN_W * 0.2
        var index: CGFloat = 0
        // 便利子控制器
        for view in subviews {
            if view.isKind(of: NSClassFromString("UITabBarButton")!) {
    
                view.frame.size.width = w
                view.frame.origin.x = index * w
                index += 1
                if index == 2 {
                    index += 1
                }
            }
        }
        // 设置加号按钮的位置
        composeButton.center.x = frame.width * 0.5
        composeButton.center.y = frame.height * 0.5
        
    }
    // 添加加号按钮
    lazy var composeButton: UIButton = {
        let button = UIButton()
        
        // 按钮添加点击事件
        button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        // 设置图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), for: UIControlState.normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        
        // 设置按钮大小
        button.sizeToFit()
        
        return button
    }()
    
}
